package org.jspresso.framework.application.frontend.action.std;

import java.util.Map;

import org.jspresso.framework.action.IActionHandler;
import org.jspresso.framework.binding.IValueConnector;
import org.jspresso.framework.binding.basic.BasicCollectionConnector;
import org.jspresso.model.ActionTestIo;
import org.jspresso.model.TestEntity;

public class DeleteTestEntityIfRequiredToViewConnectorSetAction extends SetConnectorValueAction {
	@Override
	public boolean execute(IActionHandler actionHandler, final Map context) {

		final IValueConnector connector = (IValueConnector) context.get("VIEW_CONNECTOR");
		
		if(connector instanceof BasicCollectionConnector) { // List situation, we don't want to use the super.execute()
			ActionTestIo actionTestIo = ((BasicCollectionConnector)context.get("VIEW_CONNECTOR")).getParentConnector().getConnectorValue();
			actionTestIo.addToParameterRefValues((TestEntity) getActionParameter(context));
			return true;
		}
		else {
			if(connector.getConnectorValue() != null && ((TestEntity)connector.getConnectorValue()).isGeneratedEntity()) {
				getBackendController(context).registerForDeletion((TestEntity) connector.getConnectorValue());
			}
			return super.execute(actionHandler, context);
		}

	}
}
